/**
 * 
 */
package com.ly.mpayment.persist.common;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.UUID;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

/**
 * 项目名称： 类名称: CustomUUID.java 类描述: 创建人: 
 * 备注:
 * 
 * @version
 */
public class CustomUUID implements UserType, Serializable {

	private static final long serialVersionUID = 6839779237224943510L;

	private String id;

	public CustomUUID() {
	}

	public CustomUUID(String id) {
		this.id = id;
	}

	public int[] sqlTypes() {
		return new int[] { Types.VARCHAR };
	}

	@SuppressWarnings("rawtypes")
	public Class returnedClass() {
		return CustomUUID.class;
	}

	public boolean equals(Object obj1, Object obj2) throws HibernateException {
		if (obj1 != null && obj2 != null) {
			CustomUUID customUUID = (CustomUUID) obj1;
			CustomUUID customUUID2 = (CustomUUID) obj2;

			if (null != customUUID.getId() && null != customUUID2.getId()) {
				if (customUUID.getId().equals(customUUID2.getId())) {
					return true;
				}
			}
		}
		return false;
	}

	
	public int hashCode(Object arg0) throws HibernateException {
		return 0;
	}

	
	public CustomUUID nullSafeGet(ResultSet rs, String[] names, Object ower)
			throws HibernateException, SQLException {
		CustomUUID customUUID = new CustomUUID();
		Object object = rs.getObject(names[0]);
		if (object != null) {
			customUUID.id = ((UUID) object).toString();
			return customUUID;
		}
		return null;
	}

	
	public void nullSafeSet(PreparedStatement st, Object value, int index)
			throws HibernateException, SQLException {
		if (value != null) {
			UUID cusString = null;
			if(value instanceof String){
				cusString = UUID.fromString(value.toString());
			}else if(value instanceof CustomUUID){
				CustomUUID customUUID = (CustomUUID) value;
				cusString = UUID.fromString(customUUID.getId());
			}
			st.setObject(index, cusString);
		} else {
			st.setNull(index, Types.NULL);
		}
	}

	
	public Object deepCopy(Object obj) throws HibernateException {
		CustomUUID customUUID = (CustomUUID) obj;
		CustomUUID suCustomUUIDCopy = new CustomUUID(customUUID.getId());
		return suCustomUUIDCopy;
	}

	
	public Object assemble(Serializable arg0, Object arg1)
			throws HibernateException {

		return null;
	}

	
	public boolean isMutable() {
		return false;
	}

	
	public Serializable disassemble(Object obj) throws HibernateException {
		return (CustomUUID) obj;
	}

	
	public Object replace(Object arg0, Object arg1, Object arg2)
			throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	
	public String toString() {
		return id == null ? super.toString() : id;
	}

}
